package com.itheima.controller.backend.security;

import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    /**
     * 模板下载
     * @param filename
     * @return
     * @throws FileNotFoundException
     */
    @GetMapping("/download")
    public ResponseEntity<InputStreamResource> downloadTemplate(
            @RequestParam(value = "filename", defaultValue = "ordersetting_template.xlsx") String filename)
            throws FileNotFoundException {

        // 1.获取模板文件路径
        String filePath = "D:\\Java_code\\git\\bxg-health-parent\\bxg-health-backend\\src\\main\\resources\\templates\\ordersetting_template.xlsx";
        File file = new File(filePath);

        // 2.检查文件是否存在
        if(!file.exists()){
            throw new FileNotFoundException("模板文件不存在");
        }

        // 3.设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=" + filename);

        // 4.构建响应实体
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                .body(new InputStreamResource(new FileInputStream(file)));
    }

    /**
     * 文件上传
     */
    @Autowired
    private OrderSettingService orderSettingService;

    @PostMapping("/upload")
    public ResponseEntity<Result> upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            orderSettingService.importOrderSettings(excelFile);
            return ResponseEntity.ok(new Result(true, "预约设置成功", null));
        } catch (Exception e) {
            return ResponseEntity.status(201).body(new Result(false, e.getMessage(), null));
        }
    }

    @PostMapping("/editNumberByOrderDate")
    public ResponseEntity<Result> editNumberByOrderDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByOrderDate(orderSetting);
            return ResponseEntity.ok(new Result(true, "预约设置成功", null));
        } catch (Exception e) {
            return ResponseEntity.status(201).body(new Result(false, e.getMessage(), null));
        }
    }

    /**
     * 根据年月查询对应的预约设置信息
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public ResponseEntity<Result> getOrderSettingByMonth(@RequestParam String month) {
        try {
            List<OrderSetting> data = orderSettingService.getOrderSettingByMonth(month);
            return ResponseEntity.ok(new Result(true, "获取预约设置数据成功", data));
        } catch (Exception e) {
            return ResponseEntity.status(201).body(new Result(false, e.getMessage(), null));
        }
    }

}